<?php
require_once("thamdinh/models/tvtk.php");
require_once("thamdinh/models/tvgs.php");
require_once("thamdinh/models/tochuc.php");
require_once("thamdinh/models/linhvuc.php");
require_once("thamdinh/models/level.php");
class Thamdinh_IndexController extends GenericController {

    public function thamdinhtvtkbuoc1Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->id = $id;
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        }
    }

    public function thamdinhtvtkbuoc2Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($id);
            $this->view->id = $id;
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        }
    }

    public function thamdinhtvtkbuoc3Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->level = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
            $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
            $this->view->id = $id;
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        }
    }

    public function thamdinhtvtkbuoc4Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->duan = TVTK::SelectDuAnListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));

            $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVTK_NHOMCAPCONGTRINH');
            $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVTK_NHOMCAPCONGTRINH');

            $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVTK_THONGTINDICHVU');
            $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVTK_THONGTINDICHVU');

            $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVTK_THONGTINBOSUNG');
            $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVTK_THONGTINBOSUNG');
            $this->view->id = $id;
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        }
    }
    
    public function thamdinhtvtkbuoc1savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVTK::SelectTVTKByID($id);

        // Save thong tin to chuc
        $parameter = Array(
            "NAME" => $params["NAMETOCHUC"]
            , "GPDKKD_SO" => $params["GPDKKD_SO"]
            , "GPDKKD_NGAY" => TVTKCommon::VNDateToMySQLDate($params["GPDKKD_NGAY"])
            , "TRUSO" => $params["TRUSO"]
            , "DIENTHOAI" => $params["DIENTHOAI"]
            , "DAIDIEN" => $params["DAIDIEN"]
            , "FAX" => $params["FAX"]
        );
        if((int)$this->view->tochuc["ID_TC"]>0)
        {
            ToChuc::UpdateByID_TC($parameter, $this->view->tochuc["ID_TC"]);
        }
        if ($this->view->tvtk["STATUS"] == 0) {
            $parameter = Array(
                "ID_LV" => $params["ID_LV"]
                , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
            );
            TVTK::UpdateTVTKM1Buoc1($parameter, $this->view->tvtk["ID_BK1"]);
        }
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK1" => $id
            , "BUOC1" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC1" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVTK::InsertTVTKThamDinhBuoc1($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
        }
        else {            
            TVTK::UpdateTVTKThamDinhBuoc1($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "KETQUA" => 1
            );           
            TVTK::UpdateTVTKKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        echo 1;        
        }
        exit;
    }

    
    public function thamdinhtvtkbuoc2savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $is_new = 0;

            if ($params["delete"] > 0) {
                $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["delete"], $this->view->tochuc["ID_TC"]);
                if ($nlns["STATUS"] == 2) {
                    TVTK::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK1"], 0);
                    $is_new = 2;
                } else {
                    TVTK::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK1"], 1);
                }

                $params["ID_NLNS"] = $params["delete"];
            } else {
            // validate
                if ($params["ID_LV"] == 0) {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Hạng";
                    echo json_encode($nlns);
                    exit;
                }

                if ($params["DIEM"] == 0) {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Điểm";
                    echo json_encode($nlns);
                    exit;
                }

                if ($params["HOTEN"] == "") {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Họ tên";
                    echo json_encode($nlns);
                    exit;
                }
                // che do edit
                if ($params["ID_NLNS"] > 0) {
                    $parameter = Array();
                    $parameter["HOTEN"] = $params["HOTEN"];
                    $parameter["CMND"] = $params["CMND"];
                    $parameter["NGHE"] = $params["NGHE"];
                    $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                    $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                    $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                    $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                    $parameter["ID_LV"] = $params["ID_LV"];
                    $parameter["DIEM"] = $params["DIEM"];
                    $parameter["GHICHU"] = $params["GHICHU"];
                    TVTK::UpdateNangLucNhanSuByID_NLNS($parameter, $params["ID_NLNS"], $this->view->tvtk["ID_BK1"]);
                } else {
                    $parameter = Array();
                    $parameter["HOTEN"] = $params["HOTEN"];
                    $parameter["CMND"] = $params["CMND"];
                    $parameter["NGHE"] = $params["NGHE"];
                    $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                    $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                    $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                    $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                    $parameter["ID_LV"] = $params["ID_LV"];
                    $parameter["DIEM"] = $params["DIEM"];
                    $parameter["GHICHU"] = $params["GHICHU"];
                    $params["ID_NLNS"] = TVTK::InsertNangLucNhanSuByID_NLNS($parameter, $this->view->tvtk["ID_BK1"]);
                    $is_new = 1;
                }
            }
        // Update DIEM_NANGLUCNHANSU
        TVTK::UpdateTVTKM1DIEM_NANGLUCNHANSU($this->view->tvtk["ID_BK1"]);
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK1" => $id
            , "BUOC2" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC2" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVTK::InsertTVTKThamDinhBuoc2($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
        }
        else {
            TVTK::UpdateTVTKThamDinhBuoc2($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "KETQUA" => 1
            );           
            TVTK::UpdateTVTKKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        if ($is_new != 2) {
            $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["ID_NLNS"], $this->view->tochuc["ID_TC"]);
        } else {
            $nlns = Array();
            $nlns["ID_NLNS"] = $params["ID_NLNS"];
        }
        $nlns["IS_NEW"] = $is_new;
        echo json_encode($nlns);
    }
    exit;
}

    
    public function thamdinhtvtkbuoc2loadnlnsAction() {
        $this->_helper->layout->disableLayout();
        $params = $this->getRequest()->getParams();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $this->view->tochuc = ToChuc::SelectToChucByID($id);

        // Chi load nhan su neu dung la cua to chuc dang login
        $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($nlns);        
        }
        exit;
    }
    
    public function thamdinhtvtkbuoc3savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVTK::SelectTVTKByID($id);
        $this->view->level = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
        $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));

        // get diem tvtk
        foreach ($this->view->level as $itemlevel) {
            TVTK::UpdateNangLucKhacByID_LV($itemlevel["ID_NLK"], $itemlevel["ID_LV"], $params["TVTKKHACDIEM" . $itemlevel["ID_LV"]], $params["TVTKKHACGHICHU" . $itemlevel["ID_LV"]], $this->view->tvtk["ID_BK1"]);
        }

        $trungbinh5nam = 0;
        for ($y = date('Y') - 4; $y <= date('Y'); $y++) {
            TVTK::UpdateDoanhThuByID_BM1($this->view->tvtk["ID_BK1"], $y, $params["ID_DT_YEAR_" . $y], $this->view->tochuc["ID_TC"]);
            $trungbinh5nam += $params["ID_DT_YEAR_" . $y];
        }

        $trungbinh5nam = ($trungbinh5nam / 5.0) * 2.0;
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK1" => $id
            , "BUOC3" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC3" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVTK::InsertTVTKThamDinhBuoc3($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
        }
        else {
            TVTK::UpdateTVTKThamDinhBuoc3($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        TVTK::UpdateTVTKM1DIEM_DOANHTHU(Array("DIEM_DOANHTHU" => $trungbinh5nam), $this->view->tvtk["ID_BK1"]);

        echo 1;
        }
        exit;
    }

    
    public function thamdinhtvtkbuoc4savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVTK::SelectTVTKByID($id);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $duan = TVTK::SelectDuAnByID_DuAn($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($duan["STATUS"] == 2) {
                TVTK::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK1"], 0);
                $is_new = 2;
            } else {
                TVTK::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK1"], 1);
            }

            $params["ID_DUAN"] = $params["delete"];
        } else {
            
            if ($params["DIEM_DUAN"] == 0) {
                $duan = Array();
                $duan["msg"] = "Chưa nhập Điểm";
                echo json_encode($duan);
                exit;
            }

            if ($params["TENDUAN"] == "") {
                $duan = Array();
                $duan["msg"] = "Chưa nhập tên dự án";
                echo json_encode($duan);
                exit;
            }
            // che do edit
            if ($params["ID_DUAN"] > 0) {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                TVTK::UpdateDuAnByID_DUAN($parameter, $params["ID_DUAN"], $this->view->tvtk["ID_BK1"]);
            } else {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                $params["ID_DUAN"] = TVTK::InsertDuAnByID_DUAN($parameter, $this->view->tvtk["ID_BK1"]);
                $is_new = 1;
            }
        }
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK1" => $id
            , "BUOC4" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC4" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVTK::InsertTVTKThamDinhBuoc4($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
        }
        else {            
            TVTK::UpdateTVTKThamDinhBuoc4($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "KETQUA" => 1
            );           
            TVTK::UpdateTVTKKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        // Update DIEMduan
        TVTK::UpdateTVTKM1DIEM_DUAN($this->view->tvtk["ID_BK1"]);

        if ($is_new != 2) {
            $duan = TVTK::SelectDuAnByID_DuAn($params["ID_DUAN"], $this->view->tochuc["ID_TC"]);
        } else {
            $duan = Array();
            $duan["ID_DUAN"] = $params["ID_DUAN"];
        }
        $duan["IS_NEW"] = $is_new;
        echo json_encode($duan);
        }
        exit;
    }

    
    public function thamdinhtvtkbuoc4loadduanAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID($id);

        // Chi load duan neu dung la cua to chuc dang login
        $duan= TVTK::SelectDuAnByID_DuAn($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($duan);
        }
        exit;
    }
    public function savethamdinhbuoc2tvtkAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "BUOC2" => (int)$params["THAMDINH"]
                , "GHICHU_BUOC2" => $params["GHICHUTHAMDINH"]
            );
            if (count($this->view->thamdinh) == 0) {

                TVTK::InsertTVTKThamDinhBuoc2($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
            }
            else {            
                TVTK::UpdateTVTKThamDinhBuoc2($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
            if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
            {
                 $parameterthamdinh = Array(
                    "ID_BK1" => $id
                    , "KETQUA" => 1
                );           
                TVTK::UpdateTVTKKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            echo 1;
        }
        exit;
    }
    public function savethamdinhbuoc4tvtkAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
             $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "BUOC4" => (int)$params["THAMDINH"]
                , "GHICHU_BUOC4" => $params["GHICHUTHAMDINH"]
                , "IS_CONGBO" => 0
            );
            if($params['congbo']==1)
            {
                $parameterthamdinh['IS_CONGBO']=1;
            }
            if (count($this->view->thamdinh) == 0) {

                TVTK::InsertTVTKThamDinhBuoc4($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
            }
            else {            
                TVTK::UpdateTVTKThamDinhBuoc4($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
            if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
            {
                 $parameterthamdinh = Array(
                    "ID_BK1" => $id
                    , "KETQUA" => 1
                );           
                TVTK::UpdateTVTKKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $data=array('STATUS'=>1);
            $model= new  formTVTK();
            $model->update($data, 'ID_BK1='.$this->view->tvtk["ID_BK1"]);
            echo 1;
        }
        exit;
    }
    public function khongxulytvtkAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {$this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->thamdinh = TVTK::SelectTVTKThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK1" => $id
                , "IS_KHONGTHAMDINH" => 1
            );
            if (count($this->view->thamdinh) == 0) {

                TVTK::InsertTVTKKhongthamdinh($parameterthamdinh, $this->view->tvtk["ID_BK1"]);
            }
            else {            
                TVTK::UpdateTVTKKhongthamdinh($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            echo 1;
        }
        exit;
    }
    /*-----------------------------------------------------*/
    public function thamdinhtvgsbuoc1Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->id = $id;
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        }
    }

    public function thamdinhtvgsbuoc2Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVGS_CAPHANG");
            $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($id);
            $this->view->id = $id;
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        }
    }

    public function thamdinhtvgsbuoc3Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->level = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK2"]);
            $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
            $this->view->id = $id;
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        }
    }

    public function thamdinhtvgsbuoc4Action() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->duan = TVGS::SelectDuAnListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));

            $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVGS_NHOMCAPCONGTRINH');
            $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVGS_NHOMCAPCONGTRINH');

            $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVGS_THONGTINDICHVU');
            $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVGS_THONGTINDICHVU');

            $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVGS_THONGTINBOSUNG');
            $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVGS_THONGTINBOSUNG');
            $this->view->id = $id;
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        }
    }
    
    public function thamdinhtvgsbuoc1savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVGS::SelectTVGSByID($id);

        // Save thong tin to chuc
        $parameter = Array(
            "NAME" => $params["NAMETOCHUC"]
            , "GPDKKD_SO" => $params["GPDKKD_SO"]
            , "GPDKKD_NGAY" => TVTKCommon::VNDateToMySQLDate($params["GPDKKD_NGAY"])
            , "TRUSO" => $params["TRUSO"]
            , "DIENTHOAI" => $params["DIENTHOAI"]
            , "DAIDIEN" => $params["DAIDIEN"]
            , "FAX" => $params["FAX"]
        );
        if((int)$this->view->tochuc["ID_TC"]>0)
        {
            ToChuc::UpdateByID_TC($parameter, $this->view->tochuc["ID_TC"]);
        }
        if ($this->view->tvtk["STATUS"] == 0) {
            $parameter = Array(
                "ID_LV" => $params["ID_LV"]
                , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
            );
            TVGS::UpdateTVGSM2Buoc1($parameter, $this->view->tvtk["ID_BK2"]);
        }
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK2" => $id
            , "BUOC1" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC1" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVGS::InsertTVGSThamDinhBuoc1($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
        }
        else {            
            TVGS::UpdateTVGSThamDinhBuoc1($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "KETQUA" => 1
            );           
            TVGS::UpdateTVGSKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        echo 1;        
        }
        exit;
    }

    
    public function thamdinhtvgsbuoc2savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $is_new = 0;

            if ($params["delete"] > 0) {
                $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["delete"], $this->view->tochuc["ID_TC"]);
                if ($nlns["STATUS"] == 2) {
                    TVGS::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK2"], 0);
                    $is_new = 2;
                } else {
                    TVGS::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK2"], 1);
                }

                $params["ID_NLNS"] = $params["delete"];
            } else {
            // validate
                if ($params["ID_LV"] == 0) {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Hạng";
                    echo json_encode($nlns);
                    exit;
                }

                if ($params["DIEM"] == 0) {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Điểm";
                    echo json_encode($nlns);
                    exit;
                }

                if ($params["HOTEN"] == "") {
                    $nlns = Array();
                    $nlns["msg"] = "Chưa nhập Họ tên";
                    echo json_encode($nlns);
                    exit;
                }
                // che do edit
                if ($params["ID_NLNS"] > 0) {
                    $parameter = Array();
                    $parameter["HOTEN"] = $params["HOTEN"];
                    $parameter["CMND"] = $params["CMND"];
                    $parameter["NGHE"] = $params["NGHE"];
                    $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                    $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                    $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                    $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                    $parameter["ID_LV"] = $params["ID_LV"];
                    $parameter["DIEM"] = $params["DIEM"];
                    $parameter["GHICHU"] = $params["GHICHU"];
                    TVGS::UpdateNangLucNhanSuByID_NLNS($parameter, $params["ID_NLNS"], $this->view->tvtk["ID_BK2"]);
                } else {
                    $parameter = Array();
                    $parameter["HOTEN"] = $params["HOTEN"];
                    $parameter["CMND"] = $params["CMND"];
                    $parameter["NGHE"] = $params["NGHE"];
                    $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                    $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                    $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                    $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                    $parameter["ID_LV"] = $params["ID_LV"];
                    $parameter["DIEM"] = $params["DIEM"];
                    $parameter["GHICHU"] = $params["GHICHU"];
                    $params["ID_NLNS"] = TVGS::InsertNangLucNhanSuByID_NLNS($parameter, $this->view->tvtk["ID_BK2"]);
                    $is_new = 1;
                }
            }
        // Update DIEM_NANGLUCNHANSU
        TVGS::UpdateTVGSM2DIEM_NANGLUCNHANSU($this->view->tvtk["ID_BK2"]);
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK2" => $id
            , "BUOC2" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC2" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVGS::InsertTVGSThamDinhBuoc2($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
        }
        else {
            TVGS::UpdateTVGSThamDinhBuoc2($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "KETQUA" => 1
            );           
            TVGS::UpdateTVGSKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        if ($is_new != 2) {
            $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["ID_NLNS"], $this->view->tochuc["ID_TC"]);
        } else {
            $nlns = Array();
            $nlns["ID_NLNS"] = $params["ID_NLNS"];
        }
        $nlns["IS_NEW"] = $is_new;
        echo json_encode($nlns);
    }
    exit;
}

    
    public function thamdinhtvgsbuoc2loadnlnsAction() {
        $this->_helper->layout->disableLayout();
        $params = $this->getRequest()->getParams();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $this->view->tochuc = ToChuc::SelectToChucByID($id);

        // Chi load nhan su neu dung la cua to chuc dang login
        $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($nlns);        
        }
        exit;
    }
    
    public function thamdinhtvgsbuoc3savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVGS::SelectTVGSByID($id);
        $this->view->level = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK2"]);
        $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));

        // get diem tvtk
        foreach ($this->view->level as $itemlevel) {
            TVGS::UpdateNangLucKhacByID_LV($itemlevel["ID_NLK"], $itemlevel["ID_LV"], $params["TVTKKHACDIEM" . $itemlevel["ID_LV"]], $params["TVTKKHACGHICHU" . $itemlevel["ID_LV"]], $this->view->tvtk["ID_BK2"]);
        }

        $trungbinh5nam = 0;
        for ($y = date('Y') - 4; $y <= date('Y'); $y++) {
            TVGS::UpdateDoanhThuByID_BM2($this->view->tvtk["ID_BK2"], $y, $params["ID_DT_YEAR_" . $y], $this->view->tochuc["ID_TC"]);
            $trungbinh5nam += $params["ID_DT_YEAR_" . $y];
        }

        $trungbinh5nam = ($trungbinh5nam / 5.0) * 2.0;
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK2" => $id
            , "BUOC3" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC3" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVGS::InsertTVGSThamDinhBuoc3($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
        }
        else {
            TVGS::UpdateTVGSThamDinhBuoc3($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        TVGS::UpdateTVGSM2DIEM_DOANHTHU(Array("DIEM_DOANHTHU" => $trungbinh5nam), $this->view->tvtk["ID_BK2"]);

        echo 1;
        }
        exit;
    }

    
    public function thamdinhtvgsbuoc4savedraftAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID($id);
        $this->view->tvtk = TVGS::SelectTVGSByID($id);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $duan = TVGS::SelectDuAnByID_DuAn($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($duan["STATUS"] == 2) {
                TVGS::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK2"], 0);
                $is_new = 2;
            } else {
                TVGS::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK2"], 1);
            }

            $params["ID_DUAN"] = $params["delete"];
        } else {
            
            if ($params["DIEM_DUAN"] == 0) {
                $duan = Array();
                $duan["msg"] = "Chưa nhập Điểm";
                echo json_encode($duan);
                exit;
            }

            if ($params["TENDUAN"] == "") {
                $duan = Array();
                $duan["msg"] = "Chưa nhập tên dự án";
                echo json_encode($duan);
                exit;
            }
            // che do edit
            if ($params["ID_DUAN"] > 0) {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                TVGS::UpdateDuAnByID_DUAN($parameter, $params["ID_DUAN"], $this->view->tvtk["ID_BK2"]);
            } else {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                $params["ID_DUAN"] = TVGS::InsertDuAnByID_DUAN($parameter, $this->view->tvtk["ID_BK2"]);
                $is_new = 1;
            }
        }
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        $parameterthamdinh = Array(
            "ID_BK2" => $id
            , "BUOC4" => (int)$params["THAMDINH"]
            , "GHICHU_BUOC4" => $params["GHICHUTHAMDINH"]
        );
        if (count($this->view->thamdinh) == 0) {
            
            TVGS::InsertTVGSThamDinhBuoc4($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
        }
        else {            
            TVGS::UpdateTVGSThamDinhBuoc4($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
        if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
        {
             $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "KETQUA" => 1
            );           
            TVGS::UpdateTVGSKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
        }
        // Update DIEMduan
        TVGS::UpdateTVGSM2DIEM_DUAN($this->view->tvtk["ID_BK2"]);

        if ($is_new != 2) {
            $duan = TVGS::SelectDuAnByID_DuAn($params["ID_DUAN"], $this->view->tochuc["ID_TC"]);
        } else {
            $duan = Array();
            $duan["ID_DUAN"] = $params["ID_DUAN"];
        }
        $duan["IS_NEW"] = $is_new;
        echo json_encode($duan);
        }
        exit;
    }

    
    public function thamdinhtvgsbuoc4loadduanAction() {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID($id);

        // Chi load duan neu dung la cua to chuc dang login
        $duan= TVGS::SelectDuAnByID_DuAn($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($duan);
        }
        exit;
    }
    public function savethamdinhbuoc2tvgsAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "BUOC2" => (int)$params["THAMDINH"]
                , "GHICHU_BUOC2" => $params["GHICHUTHAMDINH"]
            );
            if (count($this->view->thamdinh) == 0) {

                TVGS::InsertTVGSThamDinhBuoc2($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
            }
            else {            
                TVGS::UpdateTVGSThamDinhBuoc2($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
            if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
            {
                 $parameterthamdinh = Array(
                    "ID_BK2" => $id
                    , "KETQUA" => 1
                );           
                TVGS::UpdateTVGSKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            echo 1;
        }
        exit;
    }
    public function savethamdinhbuoc4tvgsAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $params = $this->getRequest()->getParams();

            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
             $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "BUOC4" => (int)$params["THAMDINH"]
                , "GHICHU_BUOC4" => $params["GHICHUTHAMDINH"]
                , "IS_CONGBO" => 0
            );
            if($params['congbo']==1)
            {
                $parameterthamdinh['IS_CONGBO']=1;
            }
            if (count($this->view->thamdinh) == 0) {

                TVGS::InsertTVGSThamDinhBuoc4($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
            }
            else {            
                TVGS::UpdateTVGSThamDinhBuoc4($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
            if(($this->view->thamdinh[0]["BUOC1"]==1)&&($this->view->thamdinh[0]["BUOC2"]==1)&&($this->view->thamdinh[0]["BUOC3"]==1)&&($this->view->thamdinh[0]["BUOC4"]==1))
            {
                 $parameterthamdinh = Array(
                    "ID_BK2" => $id
                    , "KETQUA" => 1
                );           
                TVGS::UpdateTVGSKetQua($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            $data=array('STATUS'=>1);
            $model= new  formTVGS();
            $model->update($data, 'ID_BK2='.$this->view->tvtk["ID_BK2"]);
            echo 1;
        }
        exit;
    }
    public function khongxulytvgsAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id)
        {$this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->thamdinh = TVGS::SelectTVGSThamDinhById($id);
            $parameterthamdinh = Array(
                "ID_BK2" => $id
                , "IS_KHONGTHAMDINH" => 1
            );
            if (count($this->view->thamdinh) == 0) {

                TVGS::InsertTVGSKhongthamdinh($parameterthamdinh, $this->view->tvtk["ID_BK2"]);
            }
            else {            
                TVGS::UpdateTVGSKhongthamdinh($parameterthamdinh, $this->view->thamdinh[0]["ID_THAMDINH"]);
            }
            echo 1;
        }
        exit;
    }
}
